<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="统计日期">
          <el-date-picker
            v-model="searchParam.statisticalDateSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getcreateTime"
          />
        </el-form-item>
        <el-form-item label="设备部门" prop="departmentId">
          <SelectOriganization
            :isCheckStrictly="true"
            :isMultiple="false"
            :selectData="searchParam.departmentId"
            @change="getSelectId"
          />
        </el-form-item>
        <el-form-item label="设备种类" prop="assetTypeId">
          <el-tree-select
            placeholder="请选择设备种类"
            v-model="searchParam.assetTypeId"
            :data="catalogList"
            node-key="id"
            :props="{
              label: 'assetTypeName',
              value: 'id',
              children: 'nodes',
            }"
            :render-after-expand="false"
            :check-strictly="true"
            clearable
          />
        </el-form-item>
        <el-form-item label="设备编码">
          <el-input
            v-model="searchParam.assetCode"
            placeholder="请输入设备编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input
            v-model="searchParam.assetName"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
      >
        <el-table-column
          label="序号"
          type="index"
          min-width="60"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备编码"
          prop="assetCode"
          min-width="150"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="设备名称"
          prop="assetName"
          min-width="150"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="设备种类"
          prop="assetTypeName"
          min-width="150"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="设备部门"
          prop="departmentName"
          min-width="150"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="运行时间(分钟)"
          prop="runningTimeSub"
          min-width="150"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ scope.row.runningTimeSub < 0 ? 0 : scope.row.runningTimeSub }}
          </template>
        </el-table-column>

        <el-table-column
          label="故障次数"
          prop="faultCount"
          min-width="180"
          show-overflow-tooltip
          align="center"
        />

        <el-table-column
          label="平均故障间隔(分钟)"
          prop="meanTimeBetweenFailures"
          min-width="150"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ parseFloat(scope.row.meanTimeBetweenFailures) }}
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { useTable } from "@/utils/useTable";
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";
import dayjs from "dayjs"; // 引入
const dictionaryStore = useDictStoreHook();
defineOptions({
  name: "EAMEQUEQUMTBF",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/eam/equStatistics/mtbf/statistics";

// 是否需要分页
const pagination = true;

// 初始化搜索参数数据
const searchParamInit = {
  assetCode: "",
  assetName: "",
  departmentId: "",
  assetTypeId: "",
  statisticalDateStart: dayjs().startOf("month").add(8, "hour"),
  statisticalDateEnd: dayjs().add(8, "hour"),
  statisticalDateSearch: [dayjs().startOf("month"), dayjs()],
};
// 数据请求错误后处理
function requestError() {}

const initParam = {};

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  handleSizeChange,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  searchParamInit,
  initParam,
  pagination,
  dataCallBack.value,
  requestError
);

function getcreateTime(data: any) {
  if (data) {
    searchParam.value.statisticalDateStart = data[0];
    searchParam.value.statisticalDateEnd = data[1];
  } else {
    searchParam.value.statisticalDateStart = "";
    searchParam.value.statisticalDateEnd = "";
  }
}

// 获取巡检班组列表
const crewList = ref([]);
const getCrewList = async () => {
  await request({
    url: "eam/eamTeam/getAvailableList",
    method: "post",
    data: {},
  }).then((res) => {
    crewList.value = res.data;
  });
};

// 目录树
const catalogList: any = ref([]);

// 获取内容树数据
const getCAtalogList = async () => {
  // 获取内容树
  await request({
    url: "eam/equAssetType/getAvailableTree",
    method: "POST",
    data: {},
  }).then((res) => {
    catalogList.value = res.data;
  });
};

onMounted(() => {
  searchParam.value.statisticalDateSearch = [dayjs().startOf("month"), dayjs()];
  searchParam.value.statisticalDateStart = dayjs()
    .startOf("month")
    .add(8, "hour");
  searchParam.value.statisticalDateEnd = dayjs().add(8, "hour");
  getCrewList();
  getTableList();
  getCAtalogList();
});

const reset = () => {
  searchParam.value = { ...searchParamInit };
  getTableList();
};
function getSelectId(id) {
  searchParam.value.departmentId = id;
}
</script>
